/**********************************************************************
 * LeechCraft - modular cross-platform feature rich internet client.
 * Copyright (C) 2006-2014  Georg Rudoy
 *
 * Distributed under the Boost Software License, Version 1.0.
 * (See accompanying file LICENSE or copy at https://www.boost.org/LICENSE_1_0.txt)
 **********************************************************************/

#pragma once

#include <memory>
#include <QObject>
#include <QUrl>
#include <sailmipim/docview/idocument.h>
#include <sailmipim/docview/ihavetoc.h>
#include <sailmipim/docview/ihavetextcontent.h>
#include <sailmipim/docview/ihavefontinfo.h>
#include <sailmipim/docview/isupportannotations.h>
#include <sailmipim/docview/isupportforms.h>
#include <sailmipim/docview/isearchabledocument.h>
#include <sailmipim/docview/isaveabledocument.h>
#include <sailmipim/docview/isupportpainting.h>
#include <sailmipim/docview/ihaveoptionalcontent.h>

namespace Poppler
{
	class Document;
}

namespace SailmiPIM
{
namespace Plugin::DocView
{
namespace PDF
{
	typedef std::shared_ptr<Poppler::Document> PDocument_ptr;

	class Document : public QObject
				   , public IDocument
				   , public IHaveTOC
				   , public IHaveTextContent
				   , public IHaveOptionalContent
				   , public IHaveFontInfo
				   , public ISupportAnnotations
				   , public ISupportForms
				   , public ISupportPainting
				   , public ISearchableDocument
				   , public ISaveableDocument
	{
		Q_OBJECT
		Q_INTERFACES (SailmiPIM::Plugin::DocView::IDocument
				SailmiPIM::Plugin::DocView::IHaveTOC
				SailmiPIM::Plugin::DocView::IHaveTextContent
				SailmiPIM::Plugin::DocView::IHaveOptionalContent
				SailmiPIM::Plugin::DocView::IHaveFontInfo
				SailmiPIM::Plugin::DocView::ISupportAnnotations
				SailmiPIM::Plugin::DocView::ISupportForms
				SailmiPIM::Plugin::DocView::ISupportPainting
				SailmiPIM::Plugin::DocView::ISearchableDocument
				SailmiPIM::Plugin::DocView::ISaveableDocument)

		PDocument_ptr PDocument_;
		TOCEntryLevel_t TOC_;
		QUrl DocURL_;

		QObject *Plugin_;
	public:
		Document (const QString&, QObject*);

		QObject* GetBackendPlugin () const;
		QObject* GetQObject ();
		bool IsValid () const;
		DocumentInfo GetDocumentInfo () const;
		int GetNumPages () const;
		QSize GetPageSize (int) const;
		QFuture<QImage> RenderPage (int, double, double);
		QList<ILink_ptr> GetPageLinks (int);
		QUrl GetDocURL () const;

		TOCEntryLevel_t GetTOC ();

		QString GetTextContent (int, const QRect&);

		QAbstractItemModel* GetOptContentModel ();

		IPendingFontInfoRequest* RequestFontInfos () const;

		QList<IAnnotation_ptr> GetAnnotations (int);

		IFormFields_t GetFormFields (int);

		void PaintPage (QPainter*, int, double, double);

		QMap<int, QList<QRectF>> GetTextPositions (const QString&, Qt::CaseSensitivity);

		SaveQueryResult CanSave () const;
		bool Save (const QString& path);

		void RequestNavigation (const QString&, int, double, double);
		void RequestPrinting ();
	private:
		void BuildTOC ();
	signals:
		void navigateRequested (const QString&, const IDocument::Position&);
		void printRequested (const QList<int>&);
	};
}
}
}
